<?php
    $tit[] = array('#','STR','C');
    $tit[] = array('AGENCIA','STR','C');
    if($tipo == 2)
        $tit[] = array('FECHA','STR','C');
    $tit[] = array('EFECTIVO','STR','C');
    $tit[] = array('CHEQUE','STR','C');
    $tit[] = array('DEPOSITO','STR','C');
    $tit[] = array('TARJETA DEBITO','STR','C');
    $tit[] = array('TARJETA CREDITO','STR','C');
    $tit[] = array("TARJETA DEBITO\rELECTRONICA",'STR','C');
    $tit[] = array("TOTAL VENTAS A\rCREDITO",'STR','C');
    $tit[] = array("TOTAL COMISIONES\rBANCARIAS",'STR','C');
    $tit[] = array("TOTAL COMISIONES\rTDC",'STR','C');
    $tit[] = array("TOTAL RETENCION\rISLR",'STR','C');
    $tit[] = array("TOTAL EFEC.+CHEQ.\r+DPTO.",'STR','C');
    $tit[] = array('TOTAL TARJETAS','STR','C');
    $tit[] = array("TOTAL VENTAS AL\rCONTADO-CREDITO\rCON COM. BANCARIA",'STR','C');
    $tit[] = array("TOTAL VENTAS AL\rCONTADO-CREDITO\rSIN COM. BANCARIA",'STR','C');
    $tit[] = array("% VENTAS SIN\rCOMISION BANCARIA",'STR','C');
    $tit[] = array("% VENTAS CON\rCOMISION BANCARIA",'STR','C');

    $this->PhpExcel->Excel();
    $ext = ($ext == 1) ? 'xlsx' : 'ods';
    $vacio = '-';
    $nombreTipo = ($tipo == 1) ? "Consolidado" : "Detallado";
    $nombre = "Cierre_de_Caja_{$nombreTipo}_".date("d_m_Y");
    $titulo="Cierre de Caja";
    $arrStyleTitle = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,
                                'startcolor' => array('argb' => '81DAF5')),
                            'font' => array('bold' => true, 'color' => array('rgb' => PHPExcel_Style_Color::COLOR_WHITE)));

    $arrStyleTotales = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,
                                'startcolor' => array('argb' => '81DAF5')),
                            'font' => array('bold' => true, 'color' => array('rgb' => PHPExcel_Style_Color::COLOR_WHITE)));

    $arrStyleTitleuno = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array('argb' => 'EFFBFB')));
    $arrborderTitleuno = array('style' => PHPExcel_Style_Border::BORDER_DASHDOT,'color' => array('rgb' => '000000'));

    if($tipo == 1) {
        $this->PhpExcel->titulo_reporte("SIGESP UTILITARIO",$titulo);
        $this->PhpExcel->titulo_hoja("REPORTE");	
        $this->PhpExcel->titulos_dinamicos(array('A5' => "DESDE: {$this->Interfaz->fecha($fechas[0])}     HASTA: {$this->Interfaz->fecha($fechas[1])}"));

        $this->PhpExcel->hoja->getDefaultStyle()->applyFromArray(array('font' => array('name' => 'arial','size' => 10)));
        $this->PhpExcel->hoja->getStyle('A1')->applyFromArray(array('font' => array('bold' => true, 'size' => 12, 'color' => array('argb' => '8B0000'))));
        $this->PhpExcel->hoja->getStyle('A2')->applyFromArray(array('font' => array('bold' => true, 'size' => 11)));
        $this->PhpExcel->hoja->getStyle('A3')->applyFromArray(array('font' => array('bold' => false,'size' => 10)));
        $this->PhpExcel->hoja->getStyle('A4')->applyFromArray(array('font' => array('bold' => false,'size' => 10)));
        $this->PhpExcel->hoja->getStyle('A5')->applyFromArray(array('font' => array('bold' => true, 'size' => 11)));

        $this->PhpExcel->configura_celda('A',5); //#
        $this->PhpExcel->configura_celda('B',30); //AGENCIA
        $this->PhpExcel->configura_celda('C',25); //EFECTIVO
        $this->PhpExcel->configura_celda('D',25); //CHEQUE
        $this->PhpExcel->configura_celda('E',25); //DEPOSITO
        $this->PhpExcel->configura_celda('F',25); //TARJETA DEBITO
        $this->PhpExcel->configura_celda('G',25); //TARJETA CREDITO
        $this->PhpExcel->configura_celda('H',25); //TARJETA DEBITO ELECTRONICA
        $this->PhpExcel->configura_celda('I',25); //TOTAL VENTAS A CREDITO
        $this->PhpExcel->configura_celda('J',25); //TOTAL COMISIONES BANCARIAS
        $this->PhpExcel->configura_celda('K',25); //TOTAL COMISIONES TDC
        $this->PhpExcel->configura_celda('L',25); //TOTAL RETENCION ISLR
        $this->PhpExcel->configura_celda('M',25); //TOTAL EFEC.+CHEQ.+DPTO
        $this->PhpExcel->configura_celda('N',25); //TOTAL TARJETAS
        $this->PhpExcel->configura_celda('O',25); //TOTAL VENTAS AL CONTADO-CREDITO CON COM. BANCARIA
        $this->PhpExcel->configura_celda('P',25); //TOTAL  VENTAS AL CONTADO-CREDITO SIN COM. BANCARIA
        $this->PhpExcel->configura_celda('Q',25); //% VENTAS SIN COMISION BANCARIA
        $this->PhpExcel->configura_celda('R',25); //% VENTAS CON COMISION BANCARIA

        $index_title = 7;
        $index_detail = 0;
        $j = 0;
		
        foreach($tit as $columnTitle) {
            $this->PhpExcel->valor_celda($j, $index_title, $columnTitle);
            $oColumn = $this->PhpExcel->obtener_columna($j, $index_title);
            $oRow = $this->PhpExcel->obtener_fila($j, $index_title);
            $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
            $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getAlignment()->setWrapText(true);
            $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->applyFromArray($arrStyleTitle);
            $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getBorders()->getAllBorders()->applyFromArray( $arrborderTitleuno);
            $j++;
        }
        $this->PhpExcel->hoja->getRowDimension($index_title)->setRowHeight(35);

        $fila = 8;
        $j = 1;
        $agencia = '';
        $cant = $efectivo = $cheque = $deposito = $tdd = 
        $tdc = $tdde = $tot_ven_cre = $tot_com_ban = 
        $por_ret_cre = $por_ret_islr = $tot_che_efe_dep = $tot_tar = 
        $tot_ven_con_cre_con_com_ban = $tot_ven_con_cre_sin_com_ban = $por_ven_sin_com_ban = $por_ven_con_com_ban = 
        $tot_ven_cre_temp = $tot_com_ban_temp = $tot_che_efe_dep_temp = $tot_tar_temp = 
        $tot_ven_con_cre_con_com_ban_temp = $tot_ven_con_cre_sin_com_ban_temp = 
        $t_efe = $t_che = $t_dep = $t_tdd = $t_tdc = $t_tdde = 
        $t_tot_ven_cre = $t_tot_com_ban = $t_por_ret_cre = $t_por_ret_islr = 
        $t_tot_che_efe_dep = $t_tot_tar = $t_tot_ven_con_cre_con_com_ban = $t_tot_ven_con_cre_sin_com_ban = 
        $t_por_ven_sin_com_ban = $t_por_ven_con_com_ban = 0;
        $total_registros = count($data);

        foreach($data as $indice => $valor2) {
            $cant++;
            if (($agencia != '') && ($agencia != $valor2['agencia'])) {
                foreach($data as $indice3 => $valor3) {
                    $tot_ven_cre_temp += ($valor3['arrime'] + $valor3['credito_fondas'] + $valor3['credito_bav'] + $valor3['creditos_varios']);
                    $tot_com_ban_temp += ($valor3['porc_ret_debito'] + $valor3['porc_ret_credito']);
                    $tot_che_efe_dep_temp += ($valor3['efectivo'] + $valor3['cheque'] + $valor3['deposito']);
                    $tot_tar_temp += ($valor3['tdd'] + $valor3['tdc'] + $valor3['tdde']);
                    $tot_ven_con_cre_con_com_ban_temp = ($tot_ven_cre_temp + $tot_com_ban_temp + $tot_che_efe_dep_temp + $tot_tar_temp);
                    $tot_ven_con_cre_sin_com_ban_temp = ($tot_ven_cre_temp + $tot_che_efe_dep_temp + $tot_tar_temp);
                }
                if($tot_ven_con_cre_sin_com_ban > 0)
                    $por_ven_sin_com_ban = ($tot_ven_con_cre_sin_com_ban * 100) / $tot_ven_con_cre_sin_com_ban_temp;
                if($tot_ven_con_cre_con_com_ban > 0)
                    $por_ven_con_com_ban = ($tot_ven_con_cre_con_com_ban * 100) / $tot_ven_con_cre_con_com_ban_temp;
                $valores[] = array($por_ven_sin_com_ban,'N', 'R');
                $valores[] = array($por_ven_con_com_ban,'N', 'R');
                $columna = 0;
                foreach($valores as $valor4) {
                    $this->PhpExcel->valor_celda($columna, $fila,  $valor4);
                    $oColumn = $this->PhpExcel->obtener_columna($columna, $fila);
                    $oRow = $this->PhpExcel->obtener_fila($columna, $fila);

                    if($j%2 == 0)
                        $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->applyFromArray($arrStyleTitleuno);

                    $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getBorders()->getAllBorders()->applyFromArray($arrborderTitleuno);
                    $columna++;
                }
                $fila++;
                $j++;

                $t_efe += $efectivo;
                $t_che += $cheque;
                $t_dep += $deposito;
                $t_tdd += $tdd;
                $t_tdc += $tdc;
                $t_tdde += $tdde;
                $t_tot_ven_cre += $tot_ven_cre;
                $t_tot_com_ban += $tot_com_ban;
                $t_por_ret_cre += $por_ret_cre;
                $t_por_ret_islr += $por_ret_islr;
                $t_tot_che_efe_dep += $tot_che_efe_dep;
                $t_tot_tar += $tot_tar;
                $t_tot_ven_con_cre_con_com_ban += $tot_ven_con_cre_con_com_ban;
                $t_tot_ven_con_cre_sin_com_ban += $tot_ven_con_cre_sin_com_ban;
                $t_por_ven_sin_com_ban += $por_ven_sin_com_ban;
                $t_por_ven_con_com_ban += $por_ven_con_com_ban;

                $efectivo = $cheque = $deposito = $tdd = 
                $tdc = $tdde = $tot_ven_cre = $tot_com_ban = 
                $por_ret_cre = $por_ret_islr = $tot_che_efe_dep = $tot_tar = 
                $tot_ven_con_cre_con_com_ban = $tot_ven_con_cre_sin_com_ban = $por_ven_sin_com_ban = $por_ven_con_com_ban = 
                $tot_ven_cre_temp = $tot_com_ban_temp = $tot_che_efe_dep_temp = $tot_tar_temp = 
                $tot_ven_con_cre_con_com_ban_temp = $tot_ven_con_cre_sin_com_ban_temp = 0;
            }

            $efectivo += $valor2['efectivo'];
            $cheque += $valor2['cheque'];
            $deposito += $valor2['deposito'];
            $tdd += $valor2['tdd'];
            $tdc += $valor2['tdc'];
            $tdde += $valor2['tdde'];
            $tot_ven_cre += ($valor2['arrime'] + $valor2['credito_fondas'] + $valor2['credito_bav'] + $valor2['creditos_varios']);
            $tot_com_ban += ($valor2['porc_ret_debito'] + $valor2['porc_ret_credito']);
            $por_ret_cre += $valor2['porc_ret_credito'];
            $por_ret_islr += $valor2['porc_ret_isrl'];
            $tot_che_efe_dep += ($valor2['efectivo'] + $valor2['cheque'] + $valor2['deposito']);
            $tot_tar += ($valor2['tdd'] + $valor2['tdc'] + $valor2['tdde']);
            $tot_ven_con_cre_con_com_ban = ($tot_ven_cre + $tot_com_ban + $tot_che_efe_dep + $tot_tar);
            $tot_ven_con_cre_sin_com_ban = ($tot_ven_cre + $tot_che_efe_dep + $tot_tar);

            $valores = array();
            $agencia = $valor2['agencia'];
            $valores[] = array($j,'STR', 'C');
            $valores[] = array($agencia,'STR', 'L');
            $valores[] = array($efectivo,'N', 'R');
            $valores[] = array($cheque, 'N', 'R');
            $valores[] = array($deposito,'N', 'R');
            $valores[] = array($tdd,'N', 'R');
            $valores[] = array($tdc,'N', 'R');
            $valores[] = array($tdde,'N', 'R');
            $valores[] = array($tot_ven_cre,'N', 'R');
            $valores[] = array($tot_com_ban,'N', 'R');
            $valores[] = array($por_ret_cre,'N', 'R');
            $valores[] = array($por_ret_islr,'N', 'R');
            $valores[] = array($tot_che_efe_dep,'N', 'R');
            $valores[] = array($tot_tar,'N', 'R');
            $valores[] = array($tot_ven_con_cre_con_com_ban,'N', 'R');
            $valores[] = array($tot_ven_con_cre_sin_com_ban,'N', 'R');

            if($total_registros == $cant){
                foreach($data as $indice3 => $valor3) {
                    $tot_ven_cre_temp += ($valor3['arrime'] + $valor3['credito_fondas'] + $valor3['credito_bav'] + $valor3['creditos_varios']);
                    $tot_com_ban_temp += ($valor3['porc_ret_debito'] + $valor3['porc_ret_credito']);
                    $tot_che_efe_dep_temp += ($valor3['efectivo'] + $valor3['cheque'] + $valor3['deposito']);
                    $tot_tar_temp += ($valor3['tdd'] + $valor3['tdc'] + $valor3['tdde']);
                    $tot_ven_con_cre_con_com_ban_temp = ($tot_ven_cre_temp + $tot_com_ban_temp + $tot_che_efe_dep_temp + $tot_tar_temp);
                    $tot_ven_con_cre_sin_com_ban_temp = ($tot_ven_cre_temp + $tot_che_efe_dep_temp + $tot_tar_temp);
                }
                if($tot_ven_con_cre_sin_com_ban > 0)
                    $por_ven_sin_com_ban = ($tot_ven_con_cre_sin_com_ban * 100) / $tot_ven_con_cre_sin_com_ban_temp;
                if($tot_ven_con_cre_con_com_ban > 0)
                    $por_ven_con_com_ban = ($tot_ven_con_cre_con_com_ban * 100) / $tot_ven_con_cre_con_com_ban_temp;
                $valores[] = array($por_ven_sin_com_ban,'N', 'R');
                $valores[] = array($por_ven_con_com_ban,'N', 'R');
                $columna = 0;
                foreach($valores as $valor4) {
                    $this->PhpExcel->valor_celda($columna, $fila,  $valor4);
                    $oColumn = $this->PhpExcel->obtener_columna($columna, $fila);
                    $oRow = $this->PhpExcel->obtener_fila($columna, $fila);

                    if($j%2 == 0)
                        $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->applyFromArray($arrStyleTitleuno);

                    $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getBorders()->getAllBorders()->applyFromArray($arrborderTitleuno);
                    $columna++;
                }
                $fila++;
                $j++;

                $t_efe += $efectivo;
                $t_che += $cheque;
                $t_dep += $deposito;
                $t_tdd += $tdd;
                $t_tdc += $tdc;
                $t_tdde += $tdde;
                $t_tot_ven_cre += $tot_ven_cre;
                $t_tot_com_ban += $tot_com_ban;
                $t_por_ret_cre += $por_ret_cre;
                $t_por_ret_islr += $por_ret_islr;
                $t_tot_che_efe_dep += $tot_che_efe_dep;
                $t_tot_tar += $tot_tar;
                $t_tot_ven_con_cre_con_com_ban += $tot_ven_con_cre_con_com_ban;
                $t_tot_ven_con_cre_sin_com_ban += $tot_ven_con_cre_sin_com_ban;
                $t_por_ven_sin_com_ban += $por_ven_sin_com_ban;
                $t_por_ven_con_com_ban += $por_ven_con_com_ban;
            }
        }
        $totales = array();
        $totales[] = array('Total:', 'STR', 'C');
        $totales[] = array($t_efe, 'N', 'R');
        $totales[] = array($t_che, 'N', 'R');
        $totales[] = array($t_dep, 'N', 'R');
        $totales[] = array($t_tdd, 'N', 'R');
        $totales[] = array($t_tdc, 'N', 'R');
        $totales[] = array($t_tdde, 'N', 'R');
        $totales[] = array($t_tot_ven_cre, 'N', 'R');
        $totales[] = array($t_tot_com_ban, 'N', 'R');
        $totales[] = array($t_por_ret_cre, 'N', 'R');
        $totales[] = array($t_por_ret_islr, 'N', 'R');
        $totales[] = array($t_tot_che_efe_dep, 'N', 'R');
        $totales[] = array($t_tot_tar, 'N', 'R');
        $totales[] = array($t_tot_ven_con_cre_con_com_ban, 'N', 'R');
        $totales[] = array($t_tot_ven_con_cre_sin_com_ban, 'N', 'R');
        $totales[] = array($t_por_ven_sin_com_ban, 'N', 'R');
        $totales[] = array($t_por_ven_con_com_ban, 'N', 'R');

        $columna = 1;
        foreach($totales as $total) {
            $this->PhpExcel->valor_celda($columna, $fila, $total);
            $oColumn = $this->PhpExcel->obtener_columna($columna, $fila);
            $oRow = $this->PhpExcel->obtener_fila($columna, $fila);
            $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->applyFromArray($arrStyleTotales);
            $this->PhpExcel->hoja->getStyle($oColumn.$oRow)->getBorders()->getAllBorders()->applyFromArray( $arrborderTitleuno);
            $columna++;
        }
    } else {
        $k = 0;
        foreach($tienda_buscar as $valor) {
            if($k > 0)
                $this->PhpExcel->xls->createSheet();
            $this->PhpExcel->xls->setActiveSheetIndex($k);
            $oac=$this->PhpExcel->xls->getActiveSheet();
            $oac->setTitle($valor);
            $oac->getDefaultStyle()->applyFromArray(array('font' => array('name' => 'arial','size' => 10)));
            $oac->getStyle('A1')->applyFromArray(array('font' => array('bold' => true, 'size' => 12, 'color' => array('argb' => '8B0000'))));
            $oac->getStyle('A2')->applyFromArray(array('font' => array('bold' => true, 'size' => 11)));
            $oac->getStyle('A3')->applyFromArray(array('font' => array('bold' => false,'size' => 10)));
            $oac->getStyle('A4')->applyFromArray(array('font' => array('bold' => false,'size' => 10)));
            $oac->setCellValue('A1', "AGROPATRIA - SIGESP UTILITARIO");
            $oac->setCellValue('A2', $this->Interfaz->mayus($titulo).' TIENDA '.$this->Interfaz->mayus($tienda[$valor]) );
            $oac->setCellValue('A3', "Fecha: ".date("d-m-Y"));
            $oac->setCellValue('A4', "Hora: ".date("h:i"));

            $oac->getColumnDimension('A')->setWidth(5); //#
            $oac->getColumnDimension('B')->setWidth(30); //AGENCIA
            $oac->getColumnDimension('C')->setWidth(20); //FECHA
            $oac->getColumnDimension('D')->setWidth(25); //EFECTIVO
            $oac->getColumnDimension('E')->setWidth(25); //CHEQUE
            $oac->getColumnDimension('F')->setWidth(25); //DEPOSITO
            $oac->getColumnDimension('G')->setWidth(25); //TARJETA DEBITO
            $oac->getColumnDimension('H')->setWidth(25); //TARJETA CREDITO
            $oac->getColumnDimension('I')->setWidth(25); //TARJETA DEBITO ELECTRONICA
            $oac->getColumnDimension('J')->setWidth(25); //TOTAL VENTAS A CREDITO
            $oac->getColumnDimension('K')->setWidth(25); //TOTAL COMISIONES BANCARIAS
            $oac->getColumnDimension('L')->setWidth(25); //TOTAL COMISIONES TDC
            $oac->getColumnDimension('M')->setWidth(25); //TOTAL RETENCION ISLR
            $oac->getColumnDimension('N')->setWidth(25); //TOTAL EFEC.+CHEQ.+DPTO
            $oac->getColumnDimension('O')->setWidth(25); //TOTAL TARJETAS
            $oac->getColumnDimension('P')->setWidth(25); //TOTAL VENTAS AL CONTADO-CREDITO CON COM. BANCARIA
            $oac->getColumnDimension('Q')->setWidth(25); //TOTAL  VENTAS AL CONTADO-CREDITO SIN COM. BANCARIA
            $oac->getColumnDimension('R')->setWidth(25); //% VENTAS SIN COMISION BANCARIA
            $oac->getColumnDimension('S')->setWidth(25); //% VENTAS CON COMISION BANCARIA	
            $index_title = 6;
            $index_detail = 0;
            $j = 0;

            foreach($tit as $columnTitle) {
                $oac->getCellByColumnAndRow($j,$index_title)->setValueExplicit($columnTitle[0], PHPExcel_Cell_DataType::TYPE_STRING);
                $oC = $oac->getCellByColumnAndRow($j, $index_title)->getColumn();
                $oR = $oac->getCellByColumnAndRow($j, $index_title)->getRow();
                $oac->getStyle($oC.$oR)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
                $oac->getStyle($oC.$oR)->getAlignment()->setWrapText(true);
                $oac->getStyle($oC.$oR)->applyFromArray($arrStyleTitle);
                $oac->getStyle($oC.$oR)->getBorders()->getAllBorders()->applyFromArray( $arrborderTitleuno);
                $j++;
            }
            $oac->getRowDimension($index_title)->setRowHeight(35);

            $fila = 7;
            $j = 1;
            $total_registros = count($data);
            if(is_array($data[$valor]) && (count($data[$valor]) > 0)) {
                $efectivo = $cheque = $deposito = $tdd = 
                $tdc = $tdde = $tot_ven_cre = $tot_com_ban = 
                $por_ret_cre = $por_ret_islr = $tot_che_efe_dep = $tot_tar = 
                $tot_ven_con_cre_con_com_ban = $tot_ven_con_cre_sin_com_ban = $por_ven_sin_com_ban = $por_ven_con_com_ban = 
                $tot_ven_cre_temp = $tot_com_ban_temp = $tot_che_efe_dep_temp = $tot_tar_temp = 
                $tot_ven_con_cre_con_com_ban_temp = $tot_ven_con_cre_sin_com_ban_temp = 
                $t_efe = $t_che = $t_dep = $t_tdd = $t_tdc = $t_tdde = 
                $t_tot_ven_cre = $t_tot_com_ban = $t_por_ret_cre = $t_por_ret_islr = 
                $t_tot_che_efe_dep = $t_tot_tar = $t_tot_ven_con_cre_con_com_ban = $t_tot_ven_con_cre_sin_com_ban = 
                $t_por_ven_sin_com_ban = $t_por_ven_con_com_ban = 0;
                foreach($data[$valor] as $valor3) {
                    $tot_ven_cre_temp += ($valor3['arrime'] + $valor3['credito_fondas'] + $valor3['credito_bav'] + $valor3['creditos_varios']);
                    $tot_com_ban_temp += ($valor3['porc_ret_debito'] + $valor3['porc_ret_credito']);
                    $tot_che_efe_dep_temp += ($valor3['efectivo'] + $valor3['cheque'] + $valor3['deposito']);
                    $tot_tar_temp += ($valor3['tdd'] + $valor3['tdc'] + $valor3['tdde']);
                    $tot_ven_con_cre_con_com_ban_temp = ($tot_ven_cre_temp + $tot_com_ban_temp + $tot_che_efe_dep_temp + $tot_tar_temp);
                    $tot_ven_con_cre_sin_com_ban_temp = ($tot_ven_cre_temp + $tot_che_efe_dep_temp + $tot_tar_temp);
                }
                foreach ($data[$valor] as $valor2) {
                    $valores = array();
                    $tot_ven_cre = ($valor2['arrime'] + $valor2['credito_fondas'] + $valor2['credito_bav'] + $valor2['creditos_varios']);
                    $tot_com_ban = ($valor2['porc_ret_debito'] + $valor2['porc_ret_credito']);
                    $tot_che_efe_dep = ($valor2['efectivo'] + $valor2['cheque'] + $valor2['deposito']);
                    $tot_tar = ($valor2['tdd'] + $valor2['tdc'] + $valor2['tdde']);
                    $tot_ven_con_cre_con_com_ban = ($tot_ven_cre + $tot_com_ban + $tot_che_efe_dep + $tot_tar);
                    $tot_ven_con_cre_sin_com_ban = ($tot_ven_cre + $tot_che_efe_dep + $tot_tar);

                    $valores[] = array($j,'STR', 'C');
                    $valores[] = array($valor2['agencia'],'STR', 'L');
                    $valores[] = array($this->Interfaz->fecha($valor2['fecha_arqueo']),'STR', 'C');
                    $valores[] = array($valor2['efectivo'],'N', 'R');
                    $valores[] = array($valor2['cheque'],'N', 'R');
                    $valores[] = array($valor2['deposito'],'N', 'R');
                    $valores[] = array($valor2['tdd'],'N', 'R');
                    $valores[] = array($valor2['tdc'],'N', 'R');
                    $valores[] = array($valor2['tdde'],'N', 'R');
                    $valores[] = array($tot_ven_cre,'N', 'R');
                    $valores[] = array($tot_com_ban,'N', 'R');
                    $valores[] = array($valor2['porc_ret_credito'],'N', 'R');
                    $valores[] = array($valor2['porc_ret_isrl'],'N', 'R');
                    $valores[] = array($tot_che_efe_dep,'N', 'R');
                    $valores[] = array($tot_tar,'N', 'R');
                    $valores[] = array($tot_ven_con_cre_con_com_ban,'N', 'R');
                    $valores[] = array($tot_ven_con_cre_sin_com_ban,'N', 'R');
                    if($tot_ven_con_cre_sin_com_ban > 0)
                        $por_ven_sin_com_ban = ($tot_ven_con_cre_sin_com_ban * 100) / $tot_ven_con_cre_sin_com_ban_temp;
                    if($tot_ven_con_cre_con_com_ban > 0)
                        $por_ven_con_com_ban = ($tot_ven_con_cre_con_com_ban * 100) / $tot_ven_con_cre_con_com_ban_temp;
                    $valores[] = array($por_ven_sin_com_ban,'N', 'R');
                    $valores[] = array($por_ven_con_com_ban,'N', 'R');

                    $t_efe += $valor2['efectivo'];
                    $t_che += $valor2['cheque'];
                    $t_dep += $valor2['deposito'];
                    $t_tdd += $valor2['tdd'];
                    $t_tdc += $valor2['tdc'];
                    $t_tdde += $valor2['tdde'];
                    $t_tot_ven_cre += $tot_ven_cre;
                    $t_tot_com_ban += $tot_com_ban;
                    $t_por_ret_cre += $valor2['porc_ret_credito'];
                    $t_por_ret_islr += $valor2['porc_ret_isrl'];
                    $t_tot_che_efe_dep += $tot_che_efe_dep;
                    $t_tot_tar += $tot_tar;
                    $t_tot_ven_con_cre_con_com_ban += $tot_ven_con_cre_con_com_ban;
                    $t_tot_ven_con_cre_sin_com_ban += $tot_ven_con_cre_sin_com_ban;
                    $t_por_ven_sin_com_ban += $por_ven_sin_com_ban;
                    $t_por_ven_con_com_ban += $por_ven_con_com_ban;
					    
                    $columna = 0;
                    foreach($valores as $valor4) {
                        if($valor4[1] == 'STR')
                            $oac->getCellByColumnAndRow($columna,$fila)->setValueExplicit($valor4[0], PHPExcel_Cell_DataType::TYPE_STRING);
                        else {
                            $oac->getCellByColumnAndRow($columna,$fila)->setValueExplicit($valor4[0], PHPExcel_Cell_DataType::TYPE_NUMERIC);
                            $oac->getStyle($oC.$oR)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
                        }

                        $oC = $oac->getCellByColumnAndRow($columna, $fila)->getColumn();
                        $oR = $oac->getCellByColumnAndRow($columna, $fila)->getRow();

                        if($valor4[2] == 'L')
                            $oac->getStyle($oC.$oR)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                        else if($valor4[2] == 'C')
                            $oac->getStyle($oC.$oR)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                        else
                            $oac->getStyle($oC.$oR)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

                        if($j%2 == 0)
                            $oac->getStyle($oC.$oR)->applyFromArray($arrStyleTitleuno);

                        $oac->getStyle($oC.$oR)->getBorders()->getAllBorders()->applyFromArray($arrborderTitleuno);
                        $columna++;					
                    }
                    $fila++;
                    $j++;
                }
                $totales = array();
                $totales[] = array('Total:', 'STR', 'C');
                $totales[] = array($t_efe, 'N', 'R');
                $totales[] = array($t_che, 'N', 'R');
                $totales[] = array($t_dep, 'N', 'R');
                $totales[] = array($t_tdd, 'N', 'R');
                $totales[] = array($t_tdc, 'N', 'R');
                $totales[] = array($t_tdde, 'N', 'R');
                $totales[] = array($t_tot_ven_cre, 'N', 'R');
                $totales[] = array($t_tot_com_ban, 'N', 'R');
                $totales[] = array($t_por_ret_cre, 'N', 'R');
                $totales[] = array($t_por_ret_islr, 'N', 'R');
                $totales[] = array($t_tot_che_efe_dep, 'N', 'R');
                $totales[] = array($t_tot_tar, 'N', 'R');
                $totales[] = array($t_tot_ven_con_cre_con_com_ban, 'N', 'R');
                $totales[] = array($t_tot_ven_con_cre_sin_com_ban, 'N', 'R');
                $totales[] = array($t_por_ven_sin_com_ban, 'N', 'R');
                $totales[] = array($t_por_ven_con_com_ban, 'N', 'R');

                $columna = 2;
                foreach($totales as $total) {
                    if($total[1] == 'STR')
                        $oac->getCellByColumnAndRow($columna,$fila)->setValueExplicit($total[0], PHPExcel_Cell_DataType::TYPE_STRING);
                     else {
                        $oac->getCellByColumnAndRow($columna,$fila)->setValueExplicit($total[0], PHPExcel_Cell_DataType::TYPE_NUMERIC);
                        $oac->getStyle($oC.$oR)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
                    }
                    $oC = $oac->getCellByColumnAndRow($columna, $fila)->getColumn();
                    $oR = $oac->getCellByColumnAndRow($columna, $fila)->getRow();

                    if($total[2] == 'L')
                        $oac->getStyle($oC.$oR)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                    else if($total[2] == 'C')
                        $oac->getStyle($oC.$oR)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    else
                        $oac->getStyle($oC.$oR)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

                    $oac->getStyle($oC.$oR)->applyFromArray($arrStyleTotales);
                    $oac->getStyle($oC.$oR)->getBorders()->getAllBorders()->applyFromArray($arrborderTitleuno);
                    $columna++;	
                }
                $k++;
            }
        }
        $this->PhpExcel->xls->setActiveSheetIndex(0);
    }
    $this->PhpExcel->mostrar($nombre,$ext);